<?php
session_start();
include_once "../apps/ManagerIUDS.php";
$managerIUDS = new ManagerIUDS();

$titleName = $_POST["title_name"];
$ownerFname = $_POST["fname"];
$ownerLname = $_POST["lname"];
$ownerAddress = $_POST["address"];
$subdistrict = $_POST["subdistrict"];
$district = $_POST["district"];
$province = $_POST["province"];
$zipCode = $_POST["zip_code"];
$telephone1 = $_POST["telephone1"];
$telephone2= $_POST["telephone2"];
$email= $_POST["email"];

$ownerId = $_GET["owner_id"];
if($ownerId == "" || $ownerId == null){
	$ownerId = $_POST["owner_id"];
}

$mode = $_GET["mode"];
if($mode == "" || $mode == null){
	$mode = $_POST["mode"];
}

$page = $_GET['page'];
if (!isset($page) && $page == "") {
	$page = 1;
}

if($mode == "insert"){
	$arrData["title_name"] = "".$titleName."";
	$arrData["fname"] = "".$ownerFname."";
	$arrData["lname"] ="".$ownerLname."";
	$arrData["address"] ="".$ownerAddress."";
	$arrData["subdistrict"] = "".$subdistrict."";
	$arrData["district"] = "".$district."";
	$arrData["province"] = "".$province."";
	$arrData["zip_code"] = "".$zipCode."";
	$arrData["telephone1"] = "".$telephone1."";
	$arrData["telephone2"] = "".$telephone2."";
	$arrData["email"] = "".$email."";
	$arrData["create_date"] = "".date("Y-m-d")."";
	$arrData["create_by"] = "".$_SESSION["user_name"]."";
	$managerIUDS->inserData($arrData, "owner");
	header("Location:../contents/owner_main.php");
}else if($mode == "delete"){
	$arrPrimaryKey["owner_id"] = "".$ownerId."";
	$managerIUDS->deleteData("owner", $arrPrimaryKey, "");
	header("Location:../contents/owner_main.php");
}else if($mode == "update"){
	$arrData["title_name"] = "".$titleName."";
	$arrData["fname"] = "".$ownerFname."";
	$arrData["lname"] ="".$ownerLname."";
	$arrData["address"] ="".$ownerAddress."";
	$arrData["subdistrict"] = "".$subdistrict."";
	$arrData["district"] = "".$district."";
	$arrData["province"] = "".$province."";
	$arrData["zip_code"] = "".$zipCode."";
	$arrData["telephone1"] = "".$telephone1."";
	$arrData["telephone2"] = "".$telephone2."";
	$arrData["email"] = "".$email."";
	$arrData["update_date"] = "".date("Y-m-d")."";
	$arrData["update_by"] = "".$_SESSION["user_name"]."";
	$arrPrimaryKey["owner_id"] = "".$ownerId."";
	//print_r($arrData);
	$managerIUDS->updateData($arrData, "owner", $arrPrimaryKey, "");
	header("Location:../contents/owner_main.php");
}else if($mode == "search"){
	$ownerName = explode(" ", $_GET["owner_name"]);
	$ownerFname = $ownerName[0];
	$ownerLname = $ownerName[1];
	header("Location:../contents/owner_main.php?mode=search&page=".$page."&owner_fname=".urlencode($ownerFname)."&owner_lname=".urlencode($ownerLname));
}else if($mode == "listOwnerData"){
	$ownerData = array();
	$ownerName = explode(" ", $_GET["owner_name"]);
	$ownerFname = $ownerName[0];
	$ownerLname = $ownerName[1];
	$ownerData = $managerIUDS->queryData("fname,lname", "owner", " WHERE fname LIKE '%".$ownerFname."%' AND lname LIKE '%".$ownerLname."%'", "2", "");
	echo json_encode($ownerData);
}
?>